﻿using Common.Infrastructure.Structs;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Common.Infrastructure
{
    public class JsonContractResolver : DefaultContractResolver
    {
        protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
        {
            return new JsonExpressionValueProvider(member);
        }

        protected override JsonProperty CreatePropertyFromConstructorParameter(JsonProperty matchingMemberProperty, ParameterInfo parameterInfo)
        {
            JsonProperty jsonProperty = base.CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
            if (jsonProperty.PropertyType.GetUnNullableType() == typeof(Guid) || jsonProperty.PropertyType.GetUnNullableType() == typeof(DateTime))
            {
                jsonProperty.NullValueHandling = NullValueHandling.Include;
            }

            return jsonProperty;
        }

        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            JsonProperty jsonProperty = base.CreateProperty(member, memberSerialization);
            if (jsonProperty.PropertyType.GetUnNullableType() == typeof(Guid) || jsonProperty.PropertyType.GetUnNullableType() == typeof(DateTime))
            {
                jsonProperty.NullValueHandling = NullValueHandling.Include;
            }

            return jsonProperty;
        }
    }
}
